package com.anolesoft.yr.mbus.fin.mapper;

import com.anolesoft.yr.mbus.fin.entity.FinBillingApplyInvoiceDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;

import java.util.List;

/**
 * @author zhj
 * @date 2020/8/12 16:58
 */
@Mapper
public interface FinBillingApplyInvoiceMapper {
    @Insert("insert into fin_billing_apply_invoice (" +
            "objuid," +
            "fac_code," +
            "fin_billing_apply_uid," +
            "invoice_code," +
            "receipt_code," +
            "amount," +
            "c_user_uid," +
            "c_time," +
            "m_user_uid," +
            "m_time," +
            "del_flag," +
            "remark" +
            ") values (" +
            "#{objuid,jdbcType=VARCHAR}," +
            "#{factoryCode,jdbcType=VARCHAR}," +
            "#{finBillingApplyUid,jdbcType=VARCHAR}," +
            "#{invoiceCode,jdbcType=VARCHAR}," +
            "#{receiptCode,jdbcType=VARCHAR}," +
            "#{amount,jdbcType=DECIMAL}," +
            "#{createUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "#{modifyUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "0," +
            "#{remark,jdbcType=VARCHAR}" +
            ")")
    int insert(FinBillingApplyInvoiceDO finBillingApplyInvoiceDO);

    @Update("update fin_billing_apply_invoice set  " +
            "fac_code = #{factoryCode,jdbcType=VARCHAR}," +
            "fin_billing_apply_uid = #{finBillingApplyUid,jdbcType=VARCHAR}," +
            "invoice_code = #{invoiceCode,jdbcType=VARCHAR}," +
            "receipt_code = #{receiptCode,jdbcType=VARCHAR}," +
            "amount = #{amount,jdbcType=DECIMAL}," +
            "m_user_uid = #{modifyUserUid,jdbcType=VARCHAR}," +
            "m_time = now()," +
            "remark = #{remark,jdbcType=VARCHAR}" +
            " where objuid = #{objuid,jdbcType=VARCHAR}")
    int update(FinBillingApplyInvoiceDO finBillingApplyInvoiceDO);

    @Update("update fin_billing_apply_invoice set del_flag = 1 where objuid = #{id,jdbcType=VARCHAR}")
    int delete(@Param("id") String id);

    @Results(id = "FinBillingApplyInvoiceResultMap", value = {
            @Result(id = true, column = "objuid", property = "objuid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fac_code", property = "factoryCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fin_billing_apply_uid", property = "finBillingApplyUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invoice_code", property = "invoiceCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "receipt_code", property = "receiptCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "amount", property = "amount", jdbcType = JdbcType.DECIMAL),
            @Result(column = "c_user_uid", property = "createUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_time", property = "createTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "m_user_uid", property = "modifyUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "m_time", property = "modifyTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "del_flag", property = "deleteFlag", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fin_billing_apply_uid", property = "billingApply", one = @One(select = "com.anolesoft.yr.mbus.fin.mapper.FinBillingApplyMapper.selectOneById", fetchType = FetchType.EAGER))
    })
    @Select("select " +
            "objuid, fac_code, fin_billing_apply_uid, invoice_code, receipt_code, amount," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from fin_billing_apply_invoice " +
            "where del_flag = 0")
    List<FinBillingApplyInvoiceDO> selectAll();

    @ResultMap("FinBillingApplyInvoiceResultMap")
    @Select("select " +
            "objuid, fac_code, fin_billing_apply_uid, invoice_code, receipt_code, amount," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from fin_billing_apply_invoice " +
            "where del_flag = 0 ")
    List<FinBillingApplyInvoiceDO> selectPageAll(Page<FinBillingApplyInvoiceDO> page);

    @ResultMap("FinBillingApplyInvoiceResultMap")
    @Select("select " +
            "objuid, fac_code, fin_billing_apply_uid, invoice_code, receipt_code, amount," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from fin_billing_apply_invoice " +
            "where del_flag = 0 and objuid = #{id,jdbcType=VARCHAR}")
    FinBillingApplyInvoiceDO selectOneById(@Param("id") String id);

    @ResultMap("FinBillingApplyInvoiceResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, fin_billing_apply_uid, invoice_code, receipt_code, amount," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from fin_billing_apply_invoice " +
            "where del_flag = 0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.finBillingApplyUid !=null and o.finBillingApplyUid != '' \">" +
            "   and fin_billing_apply_uid = #{o.finBillingApplyUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.invoiceCode !=null and o.invoiceCode != '' \">" +
            "   and invoice_code = #{o.invoiceCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.receiptCode !=null and o.receiptCode != '' \">" +
            "   and receipt_code = #{o.receiptCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<FinBillingApplyInvoiceDO> selectList(@Param("o") FinBillingApplyInvoiceDO finBillingApplyInvoiceDO);

    @ResultMap("FinBillingApplyInvoiceResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, fin_billing_apply_uid, invoice_code, receipt_code, amount," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from fin_billing_apply_invoice " +
            "where del_flag = 0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.finBillingApplyUid !=null and o.finBillingApplyUid != '' \">" +
            "   and fin_billing_apply_uid = #{o.finBillingApplyUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.invoiceCode !=null and o.invoiceCode != '' \">" +
            "   and invoice_code = #{o.invoiceCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.receiptCode !=null and o.receiptCode != '' \">" +
            "   and receipt_code = #{o.receiptCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<FinBillingApplyInvoiceDO> selectPageList(@Param("o") FinBillingApplyInvoiceDO finBillingApplyInvoiceDO, Page<FinBillingApplyInvoiceDO> page);

    @Select("select " +
            "count(objuid) from fin_billing_apply_invoice " +
            "where del_flag = 0 and fin_billing_apply_uid = #{finBillingApplyUid, jdbcType=VARCHAR}")
    Integer checkUniqueForFinBillingApplyUid(@Param("finBillingApplyUid") String finBillingApplyUid);

    @Select("select " +
            "count(objuid) from fin_billing_apply_invoice " +
            "where del_flag = 0 and invoice_code = #{invoiceCode, jdbcType=VARCHAR}")
    Integer checkUniqueForInvoiceCode(@Param("invoiceCode") String invoiceCode);

    @Select("select " +
            "count(objuid) from fin_billing_apply_invoice " +
            "where del_flag = 0 and receipt_code = #{receiptCode, jdbcType=VARCHAR}")
    Integer checkUniqueForReceiptCode(@Param("receiptCode") String receiptCode);

    @Select("select " +
            "count(objuid) from fin_billing_apply_invoice " +
            "where del_flag = 0 and fin_billing_apply_uid = #{finBillingApplyUid, jdbcType=VARCHAR} and objuid != #{id, jdbcType=VARCHAR}")
    Integer checkUniqueForFinBillingApplyUidUpdate(@Param("finBillingApplyUid") String finBillingApplyUid, @Param("id") String id);

    @Select("select " +
            "count(objuid) from fin_billing_apply_invoice " +
            "where del_flag = 0 and invoice_code = #{invoiceCode, jdbcType=VARCHAR} and objuid != #{id, jdbcType=VARCHAR}")
    Integer checkUniqueForInvoiceCodeUpdate(@Param("invoiceCode") String invoiceCode, @Param("id") String id);

    @Select("select " +
            "count(objuid) from fin_billing_apply_invoice " +
            "where del_flag = 0 and receipt_code = #{receiptCode, jdbcType=VARCHAR} and objuid != #{id, jdbcType=VARCHAR}")
    Integer checkUniqueForReceiptCodeUpdate(@Param("receiptCode") String receiptCode, @Param("id") String id);

}
